package com.lipiao.food.Activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;
import com.lipiao.food.R;
import com.lipiao.food.SectionsPagerAdapter;
import com.lipiao.food.fragments.CollectFragment;
import com.lipiao.food.fragments.FoodFragment;
import com.lipiao.food.fragments.UserFragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//ViewPager.OnPageChangeListener
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener
        , ViewPager.OnPageChangeListener {

    BottomNavigationBar mBottomNavigationBar;
    TextBadgeItem mBadgeItem;
    TextBadgeItem mBadgeItem2;
    TextBadgeItem mBadgeItem3;
    ViewPager viewPager;
    private List<Fragment> fragments;
    private static Context context;//获取context

    public static Context getContext() {
        return context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplicationContext();//初始化全局context以便其他类获取
        //initView();//初始化视图组件
        initBottomNavigationBar();//初始化底部工具栏
        initViewPager();//初始化Viewpager
    }

    private void initView() {

    }

    private void initBottomNavigationBar() {
        mBottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.icons8_rice_bowl_48px, "菜谱")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.icons8_filled_star_48px, "收藏")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem2))
                .addItem(new BottomNavigationItem(R.drawable.icons8_user_48px, "我的")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem3))//依次添加item,分别icon和名称
                .setFirstSelectedPosition(0)//设置默认选择item
                .setBarBackgroundColor(R.color.colorTabBack)
                .setTabSelectedListener(this)
                .initialise();//初始化
    }

    private void initViewPager() {
        viewPager = findViewById(R.id.view_pager_main);

        fragments = new ArrayList<Fragment>();
        fragments.add(new FoodFragment());
        fragments.add(new CollectFragment());
        fragments.add(new UserFragment());

        viewPager.setAdapter(new SectionsPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);//取消滑动
        viewPager.setOffscreenPageLimit(2);//预加载
        viewPager.setCurrentItem(0);
    }

    //BottomNavigationBar.OnTabSelectedListener接口重载
    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }

    //ViewPager.OnPageChangeListener接口重载
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        viewPager.setCurrentItem(position);
        mBottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}
